<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends MY_Controller {
    
    var $controller_path 	    = '/admin/index/';
    var $model_path 		    = 'admin/admin_model';
    var $lang_path 		    = 'admin/admin';
	
    function __construct()
    {
		parent::__construct();
        $this->load->model("admin/admin_role_model");
		log_message('debug', "admin/index Index controller Class Initialized.");
    }
	
    public function index()
    {
        $map_arr = NULL;
        $total_nums = $this->admin_model->lists(NULL,$map_arr);
        $ret_arr = $this->get_page($total_nums,$this->get_input("pageNum"),$this->get_input("pageSize"));
        $ret_arr["data"]["content"] = $this->admin_model->lists($ret_arr["limit"],$map_arr);

        if($ret_arr["data"]["content"])
        {
        	foreach($ret_arr["data"]["content"] as &$var)
        	{
        		$var->Ctime = $this->date($var->Ctime);
        		//$var->image = $this->pic2url($var->image);
        		$var->imageUrl = $this->pic2url($var->image);
        		$var->userRoles = $this->admin_role_model->get_admin_all($var->id);

                $roleNames = NULL;
        		if($var->userRoles)
                {

                    foreach($var->userRoles as $i)
                    {
                        $roleNames .= $i->remark.",";
                    }
                    $roleNames = trim($roleNames,",");
                }
                $var->roleNames = $roleNames;

        		//$var->Sex = $var->Sex?"男":"女";
        	}
        }
		$this->api("200",$ret_arr["data"],"");
    }

    public function delete()
    {
        $ret_data['type'] = 304;
        $ret_data['message'] = '';
        $ret_data['data'] = NULL;

        $ids = $this->get_input();
        $ids_arr = NULL;
        $type = false;
        if($ids)
        {
            foreach($ids as $val)
            {
                $ids_arr[] = $val->id;
            }
            $type = $this->admin_model->dels($ids_arr);
        }


        if($type) {
            $ret_data['type'] = 200;
            $ret_data['message'] = $this->lang->line('admin_success');
        }
        else {
            $ret_data['message'] = $this->lang->line('admin_error');
        }
        $this->write_logs($ret_data['type'],$ret_data['data'],"管理员删除：".$ret_data['message']);
        $this->api($ret_data['type'],$ret_data['data'],$ret_data['message']);
    }

    public function save()
    {
        $ret_data['type'] = 304;
        $ret_data['message'] = '';
        $ret_data['data'] = NULL;

        $post_data = $this->get_input("","array");

        $this->form_validation->set_data($post_data);
        $this->form_validation->dir_set_rules('admin/admin');
        if($this->form_validation->run() == true) {

            //if($post_data["id"]) $data_arr['id'] = $post_data["id"];
            if(isset($post_data["password"]) && $post_data["password"] != NULL )
            {
                $data_arr['password'] = md5($post_data["password"]);
            }

            $data_arr['name'] = $post_data["name"];
            $data_arr['dept_id'] = $post_data["deptId"];
            $data_arr['email'] = $post_data["email"];
            $data_arr['image'] = $post_data["image"];
            $data_arr['mobile'] = $post_data["mobile"];
            $data_arr['status'] = $post_data["status"];
            $data_arr['truename'] = $post_data["truename"];

            if($post_data["id"])
            {
                $userRolesArr = $post_data["userRoles"];
                if($userRolesArr)
                {
                    $add_arr = NULL;
                    foreach($userRolesArr as $val)
                    {
                        $zi_data["role_id"] = $val["roleId"];
                        $zi_data["admin_id"] = $val["userId"];
                        $add_arr[] = $zi_data;
                    }

                    //删除数据
                    if($this->admin_role_model->delect_all_data($post_data["id"]))
                    {
                        //添加数据
                        if(!$this->admin_role_model->add_all_data($add_arr))
                        {
                            $ret_data['message'] = $this->lang->line('admin_error')."add data";
                        }
                    }
                    else
                    {
                        $ret_data['message'] = $this->lang->line('admin_error')."del data";
                    }

                }
            }
            //$data_arr['email'] = $post_data["userRoles"];


            if($post_data["id"] <= 0)
            {
                $type = $this->admin_model->adds($data_arr);
            }
            else
            {
                $type = $this->admin_model->edit($post_data["id"],$data_arr);
            }

            //print_r($post_data["userRoles"]);die();
            if($type) {
                $ret_data['type'] = 200;
                $ret_data['data'] = $type;
                $ret_data['message'] = $this->lang->line('admin_success');
            }
            else {
                $ret_data['message'] = $this->lang->line('admin_error');
            }
        }
        else {
            $ret_data['message'] = $this->form_validation->error_string();
        }
        $this->write_logs($ret_data['type'],$ret_data['data'],"管理员更新：".$ret_data['message']);
        $this->api($ret_data['type'],$ret_data['data'],$ret_data['message']);
    }

}
